﻿using System;
using System.Collections.Specialized;
using System.Text;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;
using MSPE.Public.FOSS.AutoStatusPlus.Common;
using MSPE.Public.FOSS.AutoStatusPlus.StsAdm.OperationHelpers;
using MSPE.Public.FOSS.AutoStatusPlus.StsAdm.SPValidators;

namespace MSPE.Public.FOSS.AutoStatusPlus.StsAdm.Commands
{
    class AspDeleteConfig : SPOperation
    {
        #region Constructors
        /// <summary>
        /// Constructs a new instance of the <see cref="AspDeleteConfig" /> class.
        /// </summary>
        public AspDeleteConfig()
        {
            SPParamCollection _parameters = new SPParamCollection
                                               {
                                                   new SPParam(GlobalConstants.UrlParamName, GlobalConstants.UrlParamName, true, null,
                                                               new SPUrlValidator(),
                                                               "Please specify the PWA site URL.")
                                               };

            StringBuilder _sb = new StringBuilder();
            _sb.AppendLine("-url <URL of PWA Site>");
            _sb.AppendLine();
            _sb.AppendLine();
            _sb.AppendLine("\tThis command deletes any AutoStatusPlus configuration settings");
            _sb.AppendLine("\tconfigured for a PWA site. Use with extreme caution.");

            Init(_parameters, _sb.ToString());
        } // AspSetEvent()
        #endregion // Constructors

        #region Methods
        #region Overrides
        public override int Execute(string command, StringDictionary keyValues, out string output)
        {
            throw new NotImplementedException();
        } // Execute()

        /// <summary>
        /// Gets the help message.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns></returns>
        public override string GetHelpMessage(string command)
        {
            return HelpMessage;
        } // GetHelpMessage()

        /// <summary>
        /// Runs the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="keyValues">The key values.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public override int Run(string command, StringDictionary keyValues, out string output)
        {
            SPSite _site = null;
            SPWeb _pwaWeb = null;

            try
            {
                #region Read configuration settings from command line
                output = string.Empty;

                InitParameters(keyValues);

                // Read params into local fields
                string _url = Params[GlobalConstants.UrlParamName].Value.TrimEnd('/');
                bool _verbose = Params[GlobalConstants.UrlParamName].UserTypedIn;
                #endregion // Read configuration settings from command line

                // Get the PWA site
                _site = new SPSite(_url);

                if (_site.Url != _url)
                    throw new Exception("You may only run this command against Project Web Access Sites.");

                _pwaWeb = _site.RootWeb;

                if (_pwaWeb == null)
                    throw new Exception("Could not load the Site at the specified URL.");

                if (_pwaWeb.WebTemplateId != GlobalConstants.PwaSiteTemplateId)
                    throw new Exception("You may only run this command against Project Web Access Sites.");

                Utility.DeleteConfigurationKeys(_pwaWeb);

                // Use the ErrorCodes enum to return error codes: 
                // ErrorCodes.SyntaxError
                // ErrorCodes.GeneralError

                // Return 0 for success.

                return 0;
            } // End try

            catch (SoapException ex)
            {
                throw Utility.ExpandPsiException(ex);
            } // End catch

            catch (Exception ex)
            {
                throw new Exception("Error encountered while setting event:\n\n" + ex.ToString());
            } // End catch

            finally
            {
                if (_pwaWeb != null) _pwaWeb.Dispose();
                if (_site != null) _site.Dispose();
            } // End finally
        } // Run()
        #endregion // Overrides
        #endregion // Methods
    }
}
